package com.ltmonitor.web.controller.adas;

import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.util.StringUtil;
import com.ltmonitor.web.controller.command.TerminalParamAction;
import com.ltmonitor.web.util.JsonMessage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 下发Adas外设参数设置和查询指令
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/adasTerminalParam")
public class AdasTerminalParamAction extends TerminalParamAction {


	/**
	 * 打开Adas高级驾驶辅助报警参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewAdasAlarmParam.action")
	public String viewAvParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/adasTerminalParam/adasAlarmParam";
	}

	/**
	 * 打开驾驶员行为报警参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewDriverStateParam.action")
	public String viewDriverStateParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/adasTerminalParam/adasDriverStateParam";
	}


	/**
	 * 打开胎压报警参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewTirePressureParam.action")
	public String viewTirePressureParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/adasTerminalParam/adasTirePressureParam";
	}

	/**
	 * 打开胎压报警参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewBlindAreaParam.action")
	public String viewBlindAreaParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/adasTerminalParam/adasBlindAreaParam";
	}




	@ResponseBody
	@RequestMapping("/configAdasAlarmParam.action")
	public JsonMessage  configAdasAlarmParam(long vehicleId, String strAdasParam) {
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0xF364;
			sb.append(paramId).append(",");
			String sep = "|";
			sb.append(strAdasParam);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/configDriverStateParam.action")
	public JsonMessage  configDriverStateParam(long vehicleId, String strDriverStateParam)
	{
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0xF365;
			sb.append(paramId).append(",").append(strDriverStateParam);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}


	@ResponseBody
	@RequestMapping("/configTirePressureParam.action")
	public JsonMessage  configTirePressureParam(long vehicleId, String strTirePressureParam)
	{
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0xF366;
			sb.append(paramId).append(",").append(strTirePressureParam);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/configBlindAreaParam.action")
	public JsonMessage  configBlindAreaParam(long vehicleId, String strBlindAreaParam)
	{
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0xF367;
			sb.append(paramId).append(",").append(strBlindAreaParam);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}


	@ResponseBody
	@RequestMapping("/sendParamQueryCommand.action")
	public JsonMessage  sendParamQueryCommand(long vehicleId, int paramId) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(paramId);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_QUERY_SPECIAL_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}




	private TerminalCommand sendCommand(long vehicleId, String cmdData, int cmdType)
	{
		TerminalCommand tc = new TerminalCommand();
		tc.setVehicleId(vehicleId);
		tc.setCmdData(cmdData.toString());
		tc.setCmdType(cmdType);
		SendCommand(tc);
		return tc;
	}



}
